﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

namespace DatingSoonMVC2008.Library
{
    public class SimpleFileStorage : IPictureStorage
    {
        string _rootPah;

        internal SimpleFileStorage(string rootPath)
        {
            _rootPah = rootPath;
        }

        #region IPictureStorage Members

        public void Save(byte[] fileContent, int pictureID, int imageHeight)
        {
            byte[] jpegImage = Utility.Resize(fileContent, imageHeight, ImageFormat.Jpeg);
            File.WriteAllBytes(getPath(pictureID), jpegImage);
        }

        public void Delete(int pictureID)
        {
            if (File.Exists(getPath(pictureID)))
                File.Delete(getPath(pictureID));
        }

        private string getPath(int pictureID)
        {
            return _rootPah + pictureID.ToString() + ".jpg";
        }

        public string GetPictureUrl(int pictureID)
        {
            return "~/Pictures/" + pictureID.ToString() + ".jpg";
        }

        #endregion
    }
}
